package com.major.dawn.aqs.consumer;

import com.major.dawn.aqs.MyBoundedBuff;

import java.util.concurrent.atomic.AtomicInteger;

public class WaiterNotifyModel {

    private final MyBoundedBuff buff = new MyBoundedBuff();

    private final AtomicInteger atomicInteger = new AtomicInteger();

    public Runnable createConsumer() {
        Consumer consumer = new Consumer();
        return consumer;
    }


    public Runnable createProducer() {
        Producer producer = new Producer();
        return producer;
    }


    private class Consumer implements Runnable {
        @Override
        public void run() {
            try {
//                System.out.println(Thread.currentThread().toString());
                Object take = buff.take();

                System.out.println("消费者 从仓库取出 数据 :"+take.toString());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }
    }

    private class Producer implements Runnable {

        @Override
        public void run() {
            try {
//                System.out.println(Thread.currentThread().toString());
                Integer andIncrement = atomicInteger.getAndIncrement();
                buff.put(andIncrement.toString());

                System.out.println("消费者 从仓库 放入 数据 :"+andIncrement);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }



}

